﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Manas.DynamicDeepZoom.CodeCamp;
using Manas.DynamicDeepZoom.Core;
using Manas.DynamicDeepZoom.CodeCamp.ItemModels;
using System.Diagnostics;
using Manas.DynamicDeepZoom.Web.Services;
using System.Web.UI;

namespace Manas.DynamicDeepZoom.Web.Controllers
{
    [HandleError]
    public class TilesController : Controller
    {
        #region Initialization
        
        static ITileService tileService;
        static IModel model;
        static bool[] levels;

        static TilesController()
        {
            model = new ModelProvider().GetModel();
            levels = ValidLevels();

            tileService = new TileService(model, levels, 256, 256);
        }

        private static bool[] ValidLevels()
        {
            bool[] validLevels = new bool[13];
            for (int i = 0; i < validLevels.Length; i++)
            {
                validLevels[i] = (i > 3 && i != 10);
            }
            return validLevels;
        }

        public TilesController()
        {

        }

        #endregion

        public ImageResult RotatedSquares()
        {
            return new ImageResult { ImageFile = "/Images/rotated-squares.png", ImageFormat = "png" };
        }

        public ImageResult Squares()
        {
            return new ImageResult { ImageFile = "/Images/squares.png", ImageFormat = "png" };
        }

        public ActionResult Tile(int level, int x, int y)
        {
            Debug.WriteLine(string.Format("L{0}-({1},{2})", level, x, y));
            var img = tileService.GetTile(level, x, y);
            if (img == null) return new EmptyResult();
            else return new ImageResult { Image = img, ImageFormat = "png" };    
        }

        public ActionResult Level(int level)
        {
            Debug.WriteLine(string.Format("L{0})", level));
            var img = tileService.GetLevel(level);
            if (img == null) return new EmptyResult();
            else return new ImageResult { Image = img, ImageFormat = "png" };
        }
    }
}
